﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pwblog.Entities;

namespace pwblog.DataAccess
{
    public class ArticleDao
    {
        private ManageBlogDataContext dataContext;

        public ArticleDao()
        {
            dataContext = new ManageBlogDataContext();
        }

        /// <summary>
        /// lấy tất cả các bài viết được đánh dấu xuất  bản
        /// </summary>
        /// <returns></returns>
        public Article[] GetArticles()
        {
            return dataContext.Articles.Where(item => item.IsPublished == 1).ToArray();
        }

        /// <summary>
        /// lấy tất cả các bài viết
        /// </summary>
        /// <returns></returns>
        public Article[] GetArticlesAll()
        {
            return dataContext.Articles.ToArray();
        }

        /// <summary>
        /// thêm một bài viết vào cơ sở dữ liệu
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        internal int InsertArticle(Article article)
        {
            dataContext.Articles.InsertOnSubmit(article);
            dataContext.SubmitChanges();
            var temp = dataContext.Articles.ToArray();
            return temp.Last().Id;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal Article GetArticleById(int id)
        {
            return dataContext.Articles.Where(item => item.Id == id).First();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newArticle"></param>
        public void UpdateArticle(Article newArticle)
        {
            try
            {

                var oldArticle = dataContext.Articles.Single(item => item.Id == newArticle.Id);
                oldArticle.Name = newArticle.Name;
                oldArticle.DatePost = newArticle.DatePost;
                oldArticle.UserName = newArticle.UserName;
                oldArticle.IdCategory = newArticle.IdCategory;
                oldArticle.Description = newArticle.Description;
                oldArticle.Image = newArticle.Image;
                oldArticle.Viewed = newArticle.Viewed;
                oldArticle.DateModified = newArticle.DateModified;
                oldArticle.IsPublished = newArticle.IsPublished;
                oldArticle.IsPinned = newArticle.IsPinned;
                dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="article"></param>
        public void DeleteArticle(Article article)
        {
            dataContext.Articles.DeleteOnSubmit(article);
            dataContext.SubmitChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        internal Article[] GetArticlesByUserName(string userName)
        {
            var articles = dataContext.Articles.Where(item => item.UserName == userName);
            return articles.ToArray().Reverse().ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        internal Article[] GetArticlesByCategory(int id)
        {
            var articles = dataContext.Articles.Where(item => item.IdCategory == id);
            return articles.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal Article[] GetArticles(string key)
        {
            var artilces = dataContext.Articles.Where(item => item.Name.Contains(key));
            return artilces.ToArray().Reverse().ToArray();
        }
    }
}